package com.yoomiito.app.ui.college.detail

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.yoomiito.app.R
import com.yoomiito.app.model.colleage.SubjectTableInfo
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.widget.SpacingDecoration
import kotlinx.android.synthetic.main.layout_rcy.*

/**
 * 暂时替代SubjectTableFragment
 */
class SubjectTableFragment2 : androidx.fragment.app.Fragment(){
    private lateinit var mAdapter:SubjectTableAdapter
    var onItemSelectListener:(Int,SubjectTableInfo)->Unit={_,_->}

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.layout_rcy,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initRcy()
    }

    private fun initRcy() {
        layout_rcy.setBackgroundResource(R.color.color_F4F4F4)
        layout_rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        layout_rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(12f), DensityUtil.dp2px(15f), true))
        mAdapter = SubjectTableAdapter(null)
        mAdapter.setOnItemClickListener { _, _, position ->
                updateData(position)
        }
        layout_rcy.adapter = mAdapter
    }

    fun setData(subjectTableInfo: List<SubjectTableInfo>){
        mAdapter.setNewData(subjectTableInfo)
    }

    fun updateData(position:Int,datas:List<SubjectTableInfo> = mAdapter.data){
        onItemSelectListener(position,datas[position])
        for ((index,it) in datas.withIndex()){
            if (index==position){
                it.isSelected = true
            }else{
                if (it.isSelected) it.isSelected = false
            }
        }
        mAdapter.notifyDataSetChanged()
    }

    fun buySuccessUpdateData(buy: Boolean){
        val datas = mAdapter.data
        for (it in datas){
            it.isClick = buy
        }
        mAdapter.notifyDataSetChanged()
    }
}